<?php
namespace app\index\controller;
use think\Controller ;
use Db;
use Endroid\QrCode\QrCode;

class Mine extends Base
{
    public function index()
    {

        //用户信息
        $row = Db::name('user a')->field('a.*,b.level_name,b.icon,c.sales_name')
            ->leftJoin('user_level b','a.level=b.id')
            ->leftJoin('user_sales c','a.sales=c.id')
            ->where('a.id',$this->uid)->find();

        if(empty($row['level']) || $row['level_validity']<time()){
            $row['level'] = 0;
            $row['level_name'] = '普通用户';
        }

        $this->assign('info',$row);

        //最高会员
//        $vip = Db::name('user_level')->field('id,level_name')->order('price desc')->find();
//        $this->assign('vip',$vip);

        //私域流量广告
        $ad_img = Db::name('advert')->field('id,image,type,link')->where(['site'=>2,'status'=>1])->order('id desc')->find();
        $this->assign('ad_img',$ad_img);

        //充值好友
        $pay_son = Db::name('order')->where('status=1 and (pid ='.$this->uid.' or ppid='.$this->uid.')')->group('uid')->count();

        //下级会员
        $son = Db::name('user')->where('pid='.$this->uid.' or ppid='.$this->uid)->count();
        $this->assign('son',$son);
        $this->assign('pay_son',$pay_son);
        $this->assign('img_path',config('api.oss_img_path'));

        $this->assign('user',$this->userInfo);


        $this->assign('time',time());
        $explain = Db::name('config')->where('key','explain')->value('value');
        $this->assign('explain',$explain);
        return $this->fetch();
    }


    //用户等级列表
    public function level(){

        $levels = Db::name('user_level')->order('price desc')->select();
        $user_level= Db::name('user')->where('id',$this->uid)->value('level');

        $help = Db::name('help')->field('id,content')->where('status=1 and type=5')->order('id desc')->find();
        $this->assign('levels',$levels);
        $this->assign('user_level',$user_level);
        $this->assign('help',$help);

        $this->assign('phone',$this->userInfo['phone']);
        return $this->fetch();
    }

    //渠道会员
    public function salesLevel(){

        $levels = Db::name('user_sales')->order('rank desc')->select();
        $this->assign('levels',$levels);
        $this->assign('phone',$this->userInfo['phone']);

        return $this->fetch();

    }
    //用户等级列表
    public function level2(){

        $levels = Db::name('user_level')->order('price desc')->select();
        $user_level= Db::name('user')->where('id',$this->uid)->value('level');

        $help = Db::name('help')->field('id,content')->where('status=1 and type=5')->order('id desc')->find();
        $this->assign('levels',$levels);
        $this->assign('user_level',$user_level);
        $this->assign('help',$help);

        return $this->fetch();
    }


    //邀请好友
    public function invite(){

        $user = $this->userInfo;  //用户信息

        if(empty($user)){
            $invite_code = '88888';
        }else{
            $invite_code = $user['invite_code']; //邀请码

        }

        if(empty($invite_code)){
            //生成邀请码
            $invite_code = strtoupper(sprintf('%x',crc32(microtime().$user['id'])));

            $upd = Db::name('user')->where('id',$user['id'])->update(['invite_code'=>$invite_code]);
            if(!$upd){
                $this->error('生成邀请码失败,请重新稍后重试！');
            }
        }

        $this->assign('invite_code',$invite_code);


        $share = Db::name('config')->where('type','share')->column('value','key');


        $this->assign('share_bg',empty($share['share_bg'])?'':$share['share_bg']);

        //分享收益
        $share_money = empty($share['share_money'])?0:$share['share_money'];

        $this->assign('share_money',$share_money);

        //分享素材
        $share_imgs = [];

        if(!empty($share['share_imgs'])){

            $share_imgs = json_decode($share['share_imgs'],true);
        }

        $this->assign('share_imgs',$share_imgs);
        $this->assign('img_path',config('api.oss_img_path'));

        //当前域名
        $share_url=request()->domain().'/?invite_code='.$invite_code;
        $this->assign('share_url',$share_url);
//        ob_clean();



        $qrcode = $this->qrcode($share_url);

//        dump($qrcode);exit;
        $this->assign('qrcode',$qrcode);
        //二维码

        return $this->fetch();
    }


    //邀请记录
    public function inviteRecord(){

        if(request()->isAjax()){

            $uid = $this->uid;
            $type = input('type',1);

            $where = '(pid='.$uid.' OR ppid='.$uid.')';

            if($type==1){
                $where .= ' AND pay_money>0';
            }else{
                $where .= ' AND pay_money=0';

            }

            $list = Db::name('user')
                ->field('id,nickname,phone,pid,ppid,create_time')
                ->where($where)->order('id desc')
                ->paginate(15)->each(function($item, $key){
                    $item['create_time'] = date('Y-m-d H:i',$item['create_time']);
                    return $item;
                });;

//            return $list;
            return ['code'=>1,'data'=>$list,'uid'=>$uid];
        }

        //帮助说明
        $help = Db::name('help')->field('id,content')->where(['type'=>2,'status'=>1])->order('id desc')->find();
        $this->assign('help',$help);

        $count = [
            'total1'=>6,
            'total2'=>6,
        ];

        $invite['success'] = Db::name('user')->where('pay_money>0 AND (pid='.$this->uid.' OR ppid='.$this->uid.')')->count();
        $invite['wait'] = Db::name('user')->where('pay_money=0 AND (pid='.$this->uid.' OR ppid='.$this->uid.')')->count();

        $this->assign('invite',$invite);
        $this->assign('count',$count);

        return $this->fetch();
    }

    public function qrcode($url){
        $qrCode=new QrCode();

//        dump($qrCode);exit;
//        $url = 'https://www.baidu.com';//加http://这样扫码可以直接跳转url
        $qrCode->setText($url);
//            ->setSize(300)//大小
//            ->setLabelFontPath(VENDOR_PATH.'endroid\qrcode\assets\noto_sans.otf')
//            ->setErrorCorrectionLevel('high')
//            ->setForegroundColor(array('r' => 0, 'g' => 0, 'b' => 0, 'a' => 0))
//            ->setBackgroundColor(array('r' => 255, 'g' => 255, 'b' => 255, 'a' => 0))
//            ->setLabel('推广码')
//            ->setLabelFontSize(16);
//        header('Content-Type: '.$qrCode->getContentType());
        return $qrCode->writeDataUri();
    }

    //绑定手机
    public function bindPhone(){

        if(request()->isAjax()){

            $phone = input('phone');
            $code = input('code');

            if(!$phone || !$code){
                $this->error('参数有误');
            }


            $cache = cache('code_'.$phone);

            if(!$cache){
                $this->error('验证码错误');
            }

            $res = Db::name('user')->where('id',$this->uid)->update(['phone'=>$phone]);

            if($res){
                $this->success('绑定成功');
                cache('code_'.$phone,NULL);

            }else{
                $this->error('绑定失败');
            }

        }

        return $this->fetch();
    }

    //获取验证码
    public function phoneCode(){

        if(request()->isAjax()){
            $phone  = input('phone');

            if(!check_phone($phone)){
                $this->error('请输入正确的手机号码');
            }

            $row = Db::name('user')->where('phone='.$phone)->find();

            if($row){
                $this->error('此手机号已绑定,无需重复绑定');
            }

            $code = create_code();

            //发送短信
            $res = send_sms($phone,1,['code'=>$code]);

            if($res['Code']=='OK'){
                if(cache('code_'.$phone,$code,300)){
                    $this->success('发送成功');
                }else{
                    $this->error('系统繁忙，请稍后再试');
                }

            }else{
                $this->error('发送失败,请稍后再试');
            }
        }

    }


    //我的观看记录
    public function record(){

        $list = Db::name('look_record')->alias('a')
            ->field('a.*,b.image,b.movie_name')
            ->leftJoin('movie b','a.movie_id=b.id')
            ->where('uid',$this->uid)
            ->order('a.create_time desc')
            ->paginate(10);

        $this->assign('list',$list);
        $this->assign('oss_path',config('api.oss_img_path'));
        return $this->fetch();
    }


    //修改昵称
    public function updName(){

        $nickname = input('nickname');

        if(!$nickname){
            return msg(100,'昵称不能为空');
        }

        if(strlen($nickname)>15){
            return msg(100,'昵称长度不能超过15字符');
        }

        $upd = Db::name('user')->where('id',$this->uid)->update(['nickname'=>$nickname]);

        if($upd){
            return msg();
        }else{
            return msg(100,'修改失败');

        }
    }

}
